# encoding: utf-8
# 项目名称： day02
# @File  : layUI.py
# @Author: sun hao
# @Desc : 
# @Date  :   19:25 2024/10/02

from django import forms


class LayUiFormMixin:
    # 排除掉不需要继承属性的字段
    layui_exclude_fields = []
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.add_layui_attrs()

    def add_layui_attrs(self):
        for name, field in self.fields.items():
            if name in self.layui_exclude_fields:
                continue
            new_attrs = {
                "class": "layui-input",
                "placeholder": "请输入"+field.label,
                "autocomplete": "off"
            }
            if field.widget.attrs:
                field.widget.attrs.update(new_attrs)
            else:
                field.widget.attrs = new_attrs


class LayUiModelForm(LayUiFormMixin, forms.ModelForm):
    pass


class LayUiForm(LayUiFormMixin, forms.Form):
    pass
